package com.bases.ScollBar
{
	import com.bases.ImageButton;
	import com.bases.TUIComponent;
	import com.Event.ScrollBarEvent;
	import com.Timer.FrameTimer;
	import com.Timer.FrameTimerEvent;
	import com.debug.TDebug;
	import com.skin.SkinManage;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	public class TBaseScrollBar extends TUIComponent
	{
		protected var btnUp:ImageButton;
		protected var btnDown:ImageButton;
		protected var btnTrack:ImageButton;
		protected var btnScroll:ImageButton;
		
		public static var BUTTON_HEIGHT:int=13;
		public static var BUTTON_WIDTH:int=15;
		
		protected var isMouseUp:Boolean=true;
		
		protected var totalPixels : Number;
		protected var poleStartY : Number;
		
		//计时(用于鼠标放在两个按钮上)
		protected var frameTimer:FrameTimer;
		//鼠标放在按钮上面
		protected var target:Sprite;
		//down:为点按下按钮,up:为点按上按钮
		protected var type:String='';
		//是否按下两个按钮(向上或向下)
		protected var isBtnDown:Boolean=false;
		private var _w:Number;
		private var _h:Number=70;
		
		public function TBaseScrollBar()
		{
			super();
			btnUp=new ImageButton(SkinManage.getInstance().getClass('ScroolUp'),SkinManage.getInstance().getClass('ScroolUp'),
				SkinManage.getInstance().getClass('ScroolUp'));
			btnDown=new ImageButton(SkinManage.getInstance().getClass('ScroolDown'),SkinManage.getInstance().getClass('ScroolDown'),
				SkinManage.getInstance().getClass('ScroolDown'));
			btnTrack=new ImageButton(SkinManage.getInstance().getClass('ScroolTrack'),SkinManage.getInstance().getClass('ScroolTrack'),
				SkinManage.getInstance().getClass('ScroolTrack'));
			btnScroll=new ImageButton(SkinManage.getInstance().getClass('ScroolBar'),SkinManage.getInstance().getClass('ScroolBarOver'),
				SkinManage.getInstance().getClass('ScroolBarOver'));
			this.addChilds(btnUp,btnDown,btnTrack,btnScroll);
			this.setSize(20,70);
			this.btnScroll.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.btnDown.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			this.btnUp.addEventListener(MouseEvent.MOUSE_DOWN,onUp);
			this.btnTrack.addEventListener(MouseEvent.MOUSE_DOWN,onBackBGDown);
			frameTimer=new FrameTimer(200,0);
			frameTimer.addEventListener(FrameTimerEvent.TIMER,onTimer);
		}
		/**
		 * 9宫格重新设置大小
		 */		
		override protected function relayout():void{
			this.btnTrack.setSize(this.btnTrack.width,_h-2*BUTTON_HEIGHT);
			this.btnScroll.setScale9Grid(5,5,8,25);
			//this.btnScroll.setSize(this.btnScroll.width,3*BUTTON_HEIGHT);
			this.btnUp.setPos(0,0);
			this.btnDown.setPos(0,_h-BUTTON_HEIGHT);
			this.btnScroll.setPos((btnUp.width-btnScroll.width)/2,BUTTON_HEIGHT);
			this.btnTrack.setPos((btnUp.width-btnTrack.width)/2,BUTTON_HEIGHT);
			poleStartY=BUTTON_HEIGHT;
		}
		
		protected function onBackBGDown(event:MouseEvent):void{
			//点击非滑动条的位置
			var nowPosition : Number;
			if ((mouseY - this.btnUp.y) < (this.btnScroll.height / 2))
			{
				nowPosition = Math.floor(this.btnUp.y +BUTTON_HEIGHT);
			}
			else if ((this.btnDown.y - mouseY) < btnScroll.height / 2)
			{
				nowPosition = Math.floor(btnDown.y - btnScroll.height);
			}
			else
			{
				nowPosition = mouseY - btnScroll.height / 2;
			}
			btnScroll.y = nowPosition;
		}
		
		
		protected function onUp(event:MouseEvent):void{
			this.thumbScrollV();
			this.addTargetEvent(this.btnUp,'up');
		}
		
		protected function addTargetEvent(_target:Sprite,type:String):void{
			this.target=_target;
			isBtnDown=true;
			this.type=type;
			this.frameTimer.start();
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			target.addEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			this.target.addEventListener(MouseEvent.MOUSE_OUT,onDownMouseOut);
		}
		
		protected function onTargetUp(event:MouseEvent):void{
			if(isBtnDown){
				isBtnDown=false;
				this.frameTimer.stop();
				this.target.removeEventListener(MouseEvent.MOUSE_UP,onTargetUp);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			}
		}
		
		protected function onDown(event:MouseEvent):void{
			thumbScrollV();
			this.addTargetEvent(this.btnUp,'down');
		}
		
		protected function onDownMouseOut(event:MouseEvent):void{
			this.frameTimer.stop();
		}
		
		protected function onTimer(event:FrameTimerEvent):void{
			this.thumbScrollV();
		}
		
		public function setThumbHeight(value:Number,scrollV:Number,maxScrollV:Number):void{
			//根据高度因子初始化滑块的高度
			//this.btnScroll.setSize(this.btnScroll.width,Math.floor(this.btnTrack.height * Math.pow(value, 1 / 3)));
			totalPixels = Math.floor(this.btnDown.y - this.btnTrack.y - this.btnScroll.height);
			this.thumbScrollV();	
		}
		
		protected function thumbScrollV():void{
			throw new Error('请子类继承');
		}
		
		override public function setSize(w:Number, h:Number):void{
			//super.setSize(w,h);
			_w=w;
			_h=h;
			this.relayout();
		}
		
		protected function onStageMouseUp(event:MouseEvent):void{
			this.onMouseUp(event);	
		}
		
		protected function onMouseDown(event:MouseEvent):void{
			btnScroll.startDrag(false,new Rectangle(0,BUTTON_HEIGHT,0,this.height-2*BUTTON_HEIGHT-btnScroll.height));
			isMouseUp=false;
			btnScroll.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_MOVE,onStateMove);
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_UP,onStageMouseUp);
		}
		
		protected function onWheel(event:MouseEvent):void{
			thumbScrollV();
		}
		
		protected function onStateMove(event:MouseEvent):void{
		}
		
		//鼠标弹起,或鼠标离开
		protected function onMouseUp(event:MouseEvent):void{
			if(!isMouseUp){
				isMouseUp=true;
				btnScroll.stopDrag();
				btnScroll.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_MOVE,onStateMove);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_UP,onStageMouseUp);
			}
		}
		
		override public function dispose():void{
			if(!isDispose){
				super.dispose();
				this.btnScroll.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				this.btnDown.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
				this.btnUp.removeEventListener(MouseEvent.MOUSE_DOWN,onUp);
				this.btnTrack.removeEventListener(MouseEvent.MOUSE_DOWN,onBackBGDown);
				frameTimer.removeEventListener(FrameTimerEvent.TIMER,onTimer);
				this.btnScroll=null;
				this.btnDown=null;
				this.btnUp=null;
				this.btnTrack=null;
				this.frameTimer=null;
			}
		}
	}
}